package com.g2room.driving.grap.life;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jetty.util.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.g2room.driving.grap.GenericGrap;
import com.g2room.driving.grap.life.model.po.ComShopComment;
import com.g2room.driving.grap.life.model.po.LifeDriverSchool;
import com.g2room.myuall.grap.ky.model.po.GrapErr;
import com.g2room.myuall.util.DbUtil;

public class GrabJxedtDriverSchool implements Runnable {
	public static String baseUrl = "http://you.ctrip.com";
	private Map<Integer, String> ids = new HashMap<Integer, String>();
	private String url = null;
	@Override
	public void run() {
		getList(url);
	}
	public GrabJxedtDriverSchool(String url) {
		this.url= url;
	}
	
	public static void main(String[] args) throws Exception{
		String url = "http://jiaxiao.jxedt.com/cq/index_#pn#/";
		for(int i = 1; i< 20; i++) {
			GrabJxedtDriverSchool gkd = new GrabJxedtDriverSchool(url.replace("#pn#", i+""));
			Thread thread = new Thread(gkd);
			thread.start();
		}
    }
	
	public void getList(String url) {
		// 获取poiId
		Document doc = null;
		try {
			doc = GenericGrap.getDocumentByIp(url);
		} catch (IOException e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(url);
			ge.setType("1");
			ge.setDescription("获取List错误：" + e.getMessage());
			insert2ErrDb(ge);
			return;
		}
		List<LifeDriverSchool> ldses = new ArrayList<LifeDriverSchool>();
		try {
			Elements els = doc.select("#item li");
			if(null == els || els.isEmpty()) return;
			for(Element el : els) {
				LifeDriverSchool lds = new LifeDriverSchool();
				Elements tempEls = el.select(".photo a");
				if(null != tempEls && !tempEls.isEmpty()) {
					if(!StringUtil.isBlank(tempEls.first().attr("href")) && tempEls.first().attr("href").startsWith("http")) {
						lds.setSourceUrl(tempEls.first().attr("href"));
						lds.setSource("驾校一点通");
					} else {
						continue;
					}
				}
				
				 tempEls = el.select("div.intro .s0 h3 a");
				 if(null != tempEls && !tempEls.isEmpty()) {
						lds.setName(tempEls.first().text().replace("重庆", ""));
					}
				if(null != lds && null !=lds.getSourceUrl()) {
					ldses.add(lds);
				}
			} 
		} catch (Exception e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(url);
			ge.setType("3");
			ge.setDescription("获取Detail错误：" + e.getMessage());
			insert2ErrDb(ge);
			return;
		}
		List<LifeDriverSchool> ldss = new ArrayList<LifeDriverSchool>();
		for(LifeDriverSchool lds : ldses) {
			LifeDriverSchool tlds = getDetail(lds);
			if(null != tlds) ldss.add(tlds);
		}
		if(ldss.size() > 0) insert2DsDb(ldss);
	}
	
	public LifeDriverSchool getDetail(LifeDriverSchool lds) {
		// 获取poiId
		Document doc = null;
		try {
			doc = GenericGrap.getDocumentByIp(lds.getSourceUrl());
		} catch (Exception e) {
			System.out.println("==="+lds.getSourceUrl());
			e.printStackTrace();
			GrapErr ge = new GrapErr();
			ge.setUrl(lds.getSourceUrl());
			ge.setType("3");
			ge.setContent(JSON.toJSONString(lds));
			ge.setDescription("获取Detail错误：" + e.getMessage());
			insert2ErrDb(ge);
			return null;
		}
		try {
			Elements tempEls = doc.select(".content .baseinfo .photo a img");
			if(null != tempEls && !tempEls.isEmpty()) {
				lds.setCover(tempEls.first().attr("src"));
			}

			tempEls = doc.select(".cut_nav_show a");
			if(null != tempEls && !tempEls.isEmpty()) {
				lds.setCityName(tempEls.get(3).text());
			}
			
			tempEls = doc.select(".base ul li .priceinfo .price");
			if(null != tempEls && !tempEls.isEmpty()) {
				lds.setPrice(tempEls.first().text());
			}
			
			tempEls = doc.select(".base ul li .updatetime");
			if(null != tempEls && !tempEls.isEmpty()) {
				lds.setPriceDate(tempEls.first().text());
			}
			
			tempEls = doc.select(".base ul li .areaall a");
			if(null != tempEls && !tempEls.isEmpty()) {
				String str = "";
				for(Element tel : tempEls) {
					if(str.length() > 0) str += ";";
					str += tel.text();
				}
				lds.setEnrlRange(str);
			}
			
			tempEls = doc.select(".base ul li .updatetime");
			if(null != tempEls && !tempEls.isEmpty()) {
				lds.setPriceDate(tempEls.first().text());
			}
			
			tempEls = doc.select(".base ul li .address");
			if(null != tempEls && !tempEls.isEmpty()) {
				lds.setAddress(tempEls.first().text());
			}
			
			tempEls = doc.select(".base ul li .phoneandmobile");
			if(null != tempEls && !tempEls.isEmpty()) {
				lds.setPhone(tempEls.first().text());
			}
			
			tempEls = doc.select(".orderlinks .others a .yellowh");
			if(null != tempEls && !tempEls.isEmpty()) {
				lds.setReputation(tempEls.get(1).text());
			}
			
			tempEls = doc.select(".company_show");
			if(null != tempEls && !tempEls.isEmpty()) {
				Element el =tempEls.select(".infos").first();
				for(Element tel : el.children()) {
					if(tel.tagName().equals("style") || tel.attr("class").contains("topljl") || tel.text().contains("关键词")) {
						tel.remove();
					}
				}
				lds.setSummary(el.html());
				
				String title = "";
				for(Element tel : tempEls) {
					if(null == tel.select("h3") || tel.select("h3").size()==0) continue;
					title = tel.select("h3").text();
					tel.select("h3").remove();
					if(title.contains("特色")) {
						tel.select(".photos").remove();
						lds.setFeature(null != tel.select(".infos") ? tel.select(".infos").first().html() : null);
					} else if(title.contains("说明")) {
						lds.setDescription(null != tel.select(".infos") ? tel.select(".infos").first().html() : null);
					} else if(title.contains("备注")) {
						tel.select(".infos a").remove();
						lds.setRemarks(null != tel.select(".infos") ? tel.select(".infos").first().html() : null);
					}
				}
				
				lds.setTraffic(getTraffic(lds.getSourceUrl() + "bus"));
			}
			return lds;
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new GrapErr();
			ge.setUrl(url);
			ge.setType("3");
			ge.setContent(JSON.toJSONString(lds));
			ge.setDescription("获取Detail错误：" + e.getMessage());
			insert2ErrDb(ge);
			return null;
		}
	}
	
	public String getTraffic(String url) {
		// 获取poiId
		Document doc = null;
		try {
			doc = GenericGrap.getDocumentByIp(url);
		} catch (IOException e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(url);
			ge.setType("1");
			ge.setDescription("获取List错误：" + e.getMessage());
			insert2ErrDb(ge);
			return null;
		}
		try {
			Elements els = doc.select(".news_content");
			return null == els || els.isEmpty() ? null : els.first().html();
		} catch (Exception e) {
			GrapErr ge = new GrapErr();
			ge.setUrl(url);
			ge.setType("9");
			ge.setDescription("获取Detail错误：" + e.getMessage());
			insert2ErrDb(ge);
			return null;
		}
	}
	
	public void insert2DsDb(List<LifeDriverSchool> ldses) {
		String sql = "insert into life_driver_school(summary,source_url,city_id,address,name,cover,price,traffic,province_name,remarks,reputation,price_date,id,province_id,phone,description,feature,city_name,enrl_range,source) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = DbUtil.getConn("driver_school").prepareStatement(sql);
			for(LifeDriverSchool ls : ldses) {
				ps.setString(1, ls.getSummary());
				ps.setString(2, ls.getSourceUrl());
				ps.setInt(3, null == ls.getCityId() ? 0 : ls.getCityId() );
				ps.setString(4, ls.getAddress());
				ps.setString(5, ls.getName());
				ps.setString(6, ls.getCover());
				ps.setString(7, ls.getPrice());
				ps.setString(8, ls.getTraffic());
				ps.setString(9, ls.getProvinceName());
				ps.setString(10, ls.getRemarks());
				ps.setString(11, ls.getReputation());
				ps.setString(12, ls.getPriceDate());
				ps.setInt(13, null == ls.getId() ? 0 : ls.getId() );
				ps.setInt(14, null == ls.getProvinceId() ? 0 : ls.getProvinceId() );
				ps.setString(15, ls.getPhone());
				ps.setString(16, ls.getDescription());
				ps.setString(17, ls.getFeature());
				ps.setString(18, ls.getCityName());
				ps.setString(19, ls.getEnrlRange());
				ps.setString(20, ls.getSource());
				ps.addBatch();
			}	
			ps.executeBatch();	
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setType("5");
			ge.setContent(JSON.toJSONString(ldses));
			ge.setDescription("插入Detail错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		} finally {
			if(null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	private String getUtf8Str(String str) {
		try {
			str = null == str ? null : new String(str.getBytes(), "utf-8").replaceAll("[\\ud83c\\udc00-\\ud83c\\udfff]|[\\ud83d\\udc00-\\ud83d\\udfff]|[\\u2600-\\u27ff]", "");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}

	public void insert2ErrDb(GrapErr ge) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getConn("driver_school").prepareStatement("insert into grap_err(description,url,content,type) values(?,?,?,?)");
			grapPs.setString(1, ge.getDescription());
			grapPs.setString(2, ge.getUrl());
			grapPs.setString(3, ge.getContent());
			grapPs.setString(4, ge.getType());
			grapPs.execute();
			grapPs.close();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	public static List<GrapErr> getErrs() {
		List<GrapErr> univs = new ArrayList<GrapErr>();
		try {
			Connection connection = DbUtil.getConn("life");
			Statement statement = connection.createStatement();
			String sql = "select * from grap_err";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				GrapErr univ = new GrapErr();
				univ.setId(rs.getInt("id"));
				univ.setUrl(rs.getString("url"));
				univ.setType(rs.getString("type"));
				univ.setContent(rs.getString("content"));
				univs.add(univ);
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return univs;
	}
	
	public static void delErr(int id) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getComConn().prepareStatement("delete from grap_err where id=?");
			grapPs.setInt(1, id);
			grapPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					
				}
			}
		}
	}
	
	
}
